package com.sskjdata.cg.interceptor;

import com.sskjdata.cg.annotation.NoLog;
import com.sskjdata.cg.jwt.JwtUser;
import com.sskjdata.cg.service.bse.BseAccessLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * @Desciption 基础日志拦截器
 * @Author Jason
 * @Date 2019/11/30 13:13
 */
@Component
public class BseAccessLogInterceptor implements HandlerInterceptor {

    @Autowired
    private BseAccessLogService bseAccessLogService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(handler instanceof HandlerMethod){
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            NoLog annotation = handlerMethod.getBeanType().getAnnotation(NoLog.class);
            NoLog methodAnnotation = handlerMethod.getMethodAnnotation(NoLog.class);
            if(annotation==null || methodAnnotation==null){
                Method method = handlerMethod.getMethod();
                Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
                JwtUser jwtUser =null;
                if(authentication!=null && authentication.getPrincipal()!=null){
//                    jwtUser = (JwtUser) authentication.getPrincipal();
                }
                // 插入日志
//                bseAccessLogService.insertLog(request,method,jwtUser);
            }
        }
        return true;
    }
}
